package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.security.annotation.MySecurity;
import com.itheima.health.security.common.RoleConst;
import com.itheima.health.service.SetMealService;
import com.itheima.health.util.QiniuUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author spt
 * @description 套餐控制器
 * @date 2019/9/26
 **/
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetMealController {
    @Autowired
    private QiniuUtils qiniuUtils;

    @Autowired
    private SetMealService setMealService;

    /**
     * 上传图片
     * @param multipartFile
     * @return
     */
    @PostMapping("/upload")
    @MySecurity({RoleConst.ROLE_ADMIN,RoleConst.ROLE_HEALTH_MANAGER})//管理员和健康管理师
    public Result upload(@RequestParam("imgFile") MultipartFile multipartFile) {
        log.info("文件上传，name:{},size:{}", multipartFile.getOriginalFilename(), multipartFile.getSize());

        //定义文件标识
        String pic= null;
        try {
            //调用业务层进行文件上传，并返回文件标识
            pic = setMealService.upload(multipartFile);
        } catch (IOException e) {
            //图片上传失败
            return new Result(false, MessageConst.PIC_UPLOAD_FAIL);
        }
        //图片上传成功返回图片标识
        return new Result(true, MessageConst.PIC_UPLOAD_SUCCESS, pic);
    }

    /**
     * 添加套餐
     * @param setmeal   套餐基本信息
     * @param checkgroupIds 检查组ID列表
     * @return
     */
    @PostMapping("/add")
    @MySecurity({RoleConst.ROLE_ADMIN,RoleConst.ROLE_HEALTH_MANAGER})//管理员和健康管理师
    @CacheEvict(value = "setmealCache",allEntries = true) //新增体检套餐后，清空缓存
    public Result saveSetmeal(@RequestBody Setmeal setmeal , Integer[] checkgroupIds) {
        log.info("[套餐-添加]data:{},checkgroupIds:{}",setmeal,checkgroupIds);
        if(!StringUtils.isEmpty(setmeal.getImg())) {
            String img =setmeal.getImg().replace(qiniuUtils.getUrlPrefix(),"");
            setmeal.setImg(img);
        }

        //RPC调用添加数据
        setMealService.saveSetmeal(setmeal,checkgroupIds);
        return new Result(true,MessageConst.ADD_SETMEAL_SUCCESS);
    }

    /**
     * 分页查询
     * @return
     */
    @GetMapping("/findPage")
    //开启分页查询缓存
    @Cacheable(value = "setmealCache", key = "#queryPageBean.currentPage",condition = "#queryPageBean.queryString==null || #queryPageBean.queryString==''")
    public Result findPage(QueryPageBean queryPageBean){
        //调用DAO层查询并返回
        PageResult pageResult = setMealService.findPage(queryPageBean);
        //构造返回结果并返回
        return new Result(true,MessageConst.QUERY_SETMEAL_SUCCESS,pageResult);
    }
}
